/*
This file is part of buco.  
Copyright (c) 2012 Christopher Pawelzik <christopher@cpawelzik.com>

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
*/ 
package buco.jsonrpc;

import java.io.IOException;
import java.io.Writer;

import org.json.simple.*;

import buco.core.api.*;

public class BundleConverter implements JSONStreamAware {
	
	Bundle bundle; 
	
	public BundleConverter(Bundle bundle) {
		this.bundle = bundle; 
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void writeJSONString(Writer wr) throws IOException {
		JSONObject bnd = new JSONObject();
		JSONArray requirements = new JSONArray();
		JSONArray capabilities = new JSONArray();
		
		bnd.put("bundleName", bundle.getBundleName());		
		bnd.put("bundleSymbolicName", bundle.getBundleSymbolicName());
		bnd.put("bundleVersion", bundle.getBundleVersion().toString()); 
		bnd.put("bundleDescription", bundle.getBundleDescription()); 
		bnd.put("bundleID", bundle.getBundleID());
		bnd.put("top", bundle.getCustomProperty("top"));
		bnd.put("left", bundle.getCustomProperty("left"));
		if(bundle.getBcl() != null) {			
			bnd.put("bcl", bundle.getBcl());
		}
		
		// Add Requirements
		for(Requirement req: bundle.getRequirements()) {
			requirements.add(new RequirementConverter(req)); 
		}	
		
		// Add Capabilities 
		for(Capability cap : bundle.getCapabilities()) {
			capabilities.add(new CapabilityConverter(cap)); 
		}
		
		bnd.put("requirements", requirements);
		bnd.put("capabilities", capabilities); 
		
		bnd.writeJSONString(wr);  		
	}

}
